﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Novel.Entity;
using System.Data.SqlClient;
using System.Data;
using Novel.DAL.Base;

namespace Novel.DAL
{
    public class Novel_Chapter_DAL : INovel_ChapterManager
    {
        string TableName;

        public void GetTableName()
        { 
        
        }

        #region 插入记录
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <returns></returns>
        public int Insert(Novel_ChapterEntity model)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.AppendFormat("INSERT INTO {0}", TableName);
			strSql.Append("(ChapterNo,BookID,ChapterTitle,ChapterContent,Length,Path)");
			strSql.Append(" values ");
			strSql.Append("(@ChapterNo,@BookID,@ChapterTitle,@ChapterContent,@Length,@Path)");
			strSql.Append(" SELECT @ChapterID = @@IDENTITY");
			SqlCommand cmd = new SqlCommand(strSql.ToString());
			cmd.Parameters.Add("@ChapterID",SqlDbType.Int);
			cmd.Parameters["@ChapterID"].Direction = ParameterDirection.Output;
		
			cmd.Parameters.Add("@ChapterNo",SqlDbType.NVarChar);
			cmd.Parameters["@ChapterNo"].Value = model.ChapterNo;
			cmd.Parameters.Add("@BookID",SqlDbType.Int,4);
			cmd.Parameters["@BookID"].Value = model.BookID;
			cmd.Parameters.Add("@ChapterTitle",SqlDbType.NVarChar);
			cmd.Parameters["@ChapterTitle"].Value = model.ChapterTitle;
			cmd.Parameters.Add("@ChapterContent",SqlDbType.Text);
			cmd.Parameters["@ChapterContent"].Value = model.ChapterContent;
			cmd.Parameters.Add("@Length",SqlDbType.Int,4);
			cmd.Parameters["@Length"].Value = model.Length;
			cmd.Parameters.Add("@Path",SqlDbType.NVarChar);
			cmd.Parameters["@Path"].Value = model.Path;
		
			SQLHelper da = new SQLHelper();
			int EffectRows = da.ExecuteNonQuery(cmd);
            model.ChapterID = (int)cmd.Parameters["@ChapterID"].Value;
			return EffectRows;
		}
        #endregion

        #region 更新数据
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <returns></returns>
        public int Update(Novel_ChapterEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("UPDATE {0} SET ", TableName);
            strSql.Append("ChapterNo=@ChapterNo,BookID=@BookID,ChapterTitle=@ChapterTitle,ChapterContent=@ChapterContent,Length=@Length,Path=@Path");
            strSql.Append(" WHERE ChapterID =@ChapterID");
            SqlCommand cmd = new SqlCommand(strSql.ToString());
            cmd.Parameters.Add("@ChapterID", SqlDbType.Int, 4);
            cmd.Parameters["@ChapterID"].Value = model.ChapterID;
            cmd.Parameters.Add("@ChapterNo", SqlDbType.NVarChar);
            cmd.Parameters["@ChapterNo"].Value = model.ChapterNo;
            cmd.Parameters.Add("@BookID", SqlDbType.Int, 4);
            cmd.Parameters["@BookID"].Value = model.BookID;
            cmd.Parameters.Add("@ChapterTitle", SqlDbType.NVarChar);
            cmd.Parameters["@ChapterTitle"].Value = model.ChapterTitle;
            cmd.Parameters.Add("@ChapterContent", SqlDbType.Text);
            cmd.Parameters["@ChapterContent"].Value = model.ChapterContent;
            cmd.Parameters.Add("@Length", SqlDbType.Int, 4);
            cmd.Parameters["@Length"].Value = model.Length;
            cmd.Parameters.Add("@Path", SqlDbType.NVarChar);
            cmd.Parameters["@Path"].Value = model.Path;
       
            SQLHelper da = new SQLHelper();
            return da.ExecuteNonQuery(cmd);
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public int Delete(int id)
        {
            string sql = string.Format("DELETE {0} WHERE ChapterID=@ChapterID", TableName);
            SqlCommand command = new SqlCommand(sql);
            command.Parameters.Add("@ChapterID", SqlDbType.Int, 4);
            command.Parameters["@ChapterID"].Value = id;
            SQLHelper da = new SQLHelper();
            return da.ExecuteNonQuery(command);
        }
        #endregion

        #region IEntityFacade 成员
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="reader"></param>
        public EntityBase FillDetailWithReader(IDataReader reader)
        {
            Novel_ChapterEntity model = new Novel_ChapterEntity();
            if (reader["ChapterID"] != DBNull.Value)
                model.ChapterID = (int)reader["ChapterID"];
            if (reader["ChapterNo"] != DBNull.Value)
                model.ChapterNo = (string)reader["ChapterNo"];
            if (reader["BookID"] != DBNull.Value)
                model.BookID = (int)reader["BookID"];
            if (reader["ChapterTitle"] != DBNull.Value)
                model.ChapterTitle = (string)reader["ChapterTitle"];
            if (reader["ChapterContent"] != DBNull.Value)
                model.ChapterContent = (string)reader["ChapterContent"];
            if (reader["Length"] != DBNull.Value)
                model.Length = (int)reader["Length"];
            if (reader["Path"] != DBNull.Value)
                model.Path = (string)reader["Path"];
            if (reader["Addtime"] != DBNull.Value)
                model.Addtime = (DateTime)reader["Addtime"];
            return model;
        }
        #endregion

        public Novel_ChapterEntity GetObjectByID(int ID)
        {
            SQLHelper da = new SQLHelper();
            string strSql = string.Format("select * {0} WHERE BookID=@BookID", TableName);
            SQLHelper.MappEntity<Novel_ChapterEntity> dele_Entity = delegate(SqlDataReader reader)
            {
                return (Novel_ChapterEntity)FillDetailWithReader(reader);
            };
            return da.Exceute<Novel_ChapterEntity>(new SqlCommand(strSql), dele_Entity);

        }


        #region 数据集
        public DataTable ShowTable()
        {

            string strSql = string.Format("SELECT  * FROM {0}", TableName);
            return SQLHelper.Execute(strSql.ToString());
        }
        #endregion

        #region 分页数据集
        public DataTable ShowTable(PageEntity entity, out int totalPages, out int totalCount)
        {
            QueryBase manager = new QueryBase();

            entity.tbName = TableName;
            DataTable dt = manager.GetTable(entity);
            totalPages = entity.TotalPages;
            totalCount = entity.TotalCount;
            return dt;
        }
        #endregion
    }
}
